package com.int_tech.user.controller;

import com.int_tech.user.entity.User;
import com.int_tech.user.service.UserService;
import com.int_tech.utils.KuaYu;
import com.int_tech.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/12/10.
 */
@CrossOrigin       //springBoot跨域解决
@RestController
@RequestMapping(value = "user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    //查询账号密码是否正确
    @RequestMapping(value = "userLogin")
    public Result userLogin (String name , String password,HttpServletRequest request, HttpServletResponse response){
        KuaYu.kuaYuResponse(response);
        Result result = new Result(-1,"即将处理！");
        if (name != null && password != null){
            User user = userService.findUserByNameAndPassword(name,password);
            if (user!= null){
                 result = new Result(user);
            }else{
                result = new Result(-1,"输入的账号密码不正确！");
            }
        }else{
            result = new Result(-1,"账号密码为空！");
        }
        return result;
    }


    //修改密码
    @RequestMapping(value = "changeUserPassword")
    public Result changgeUserPassword (Integer userId ,String password,HttpServletRequest request, HttpServletResponse response){
        KuaYu.kuaYuResponse(response);
        Result result = new Result(-1,"即将处理！");
        if (userId != null){
            User user = userService.findUserById(userId);
            if (user != null){
                user.setPassword(password);
                try {
                    userService.saveUser(user);
                    result =  new Result(user);
                }catch (Exception e){
                    logger.info("【修改密码】【存储用户异常】"+user);
                }
            }else {
                result = new Result(-1,"输入的账号密码不正确！");
            }
        }else{
            result = new Result(-1,"用户id为空！");
        }
        return result;
    }

}
